/**
 * Created by lenovo on 2017/12/28.
 */
define(['ngApp', 'ngHttp', 'jquery', 'ngConstant', 'ngAlert', 'bootstrap'], function (ngApp, ngHttp, JQ, ngConstant, ngAlert) {
    ngApp.directive('onFinishRenderFilters', function ($timeout) {
        return {
            restrict: 'A',
            link: function (scope, element, attr) {
                if (scope.$last === true) {
                    $timeout(function () {
                        scope.$emit('ngRepeatFinished');
                    });
                }
            }
        }
    });
    ngApp.controller("leftTab8", function ($scope, $http, $timeout, $rootScope, $state, $uibModal, commonFn, $cookies, $window) {
        $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
            $rootScope.leftTabHeight = $('#leftTab8').height();
            $('.tabBox').height($rootScope.leftTabHeight);
            console.log($rootScope.leftTabHeight);
        });

        $scope.searchMsg = {};
        $scope.searchMsg.size = ngConstant.size;
        $scope.searchClicked = false;
        $scope.searcBtnClick = function(){
            $scope.searchClicked = true;
            $http({
                url: ngConstant.interfaceHost + 'manager/carBrand/findSearch',
                method: 'GET',
                params:$scope.searchMsg,
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                // if(res.data.data.content.length == 0) {
                //     ngAlert.successMsg("暂无数据");
                // } else {
                //     $scope.pageMsg = res.data.data;
                //     $scope.brandTable = res.data.data.content;
                // }

                $scope.pageMsg = res.data.data;
                $scope.brandTable = res.data.data.content;

            });
        };

        //查询数据的函数封装
        function getTableData(){
            if (!$scope.searchClicked) {
                    //查询table中所有数据接口
            $http({
                url: ngConstant.interfaceHost + 'manager/carBrand/findSearch',
                method: 'GET',
                params:{
                    "sort":"leadingChar,asc",
                    "page":$scope.page,
                    "size":ngConstant.size
                },
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                //页数数据
                $scope.pageMsg = res.data.data;
                console.log(res.data.data.content);
                //表格数据
                $scope.brandTable = res.data.data.content;
                //全选
                $scope.checked = [];
                $scope.checkedMsg = [];
                $scope.selectAll = function () {
                    if($scope.select_all) {
                        $scope.checked = [];
                        $scope.checkedMsg = [];
                        angular.forEach($scope.brandTable, function (i) {
                            i.checked = true;
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        })
                    }else {
                        angular.forEach($scope.brandTable, function (i) {
                            i.checked = false;
                            $scope.checked = [];
                            $scope.checkedMsg = [];
                        })
                    }
                };
                $scope.selectOne = function ($event) {
                    console.log($event);
                    angular.forEach($scope.brandTable, function (i) {
                        var index = $scope.checked.indexOf(i.id);
                        if(i.checked && index === -1) {
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        } else if (!i.checked && index !== -1){
                            $scope.checked.splice(index, 1);
                            $scope.checkedMsg.splice(index, 1);
                        }
                    });

                    if ($scope.brandTable.length === $scope.checked.length) {
                        $scope.select_all = true;
                    } else {
                        $scope.select_all = false;
                    }
                };
            });
            } else {
                $scope.searchMsg.page = $scope.page;
                $scope.searchMsg.size = ngConstant.size;
                //查询table中所有数据接口
            $http({
                url: ngConstant.interfaceHost + 'manager/carBrand/findSearch',
                method: 'GET',
                params:$scope.searchMsg,
                headers: {'Authorization':$cookies.get("token")}
            }).then(function(res){
                //页数数据
                $scope.pageMsg = res.data.data;
                console.log(res.data.data.content);
                //表格数据
                $scope.brandTable = res.data.data.content;
                //全选
                $scope.checked = [];
                $scope.checkedMsg = [];
                $scope.selectAll = function () {
                    if($scope.select_all) {
                        $scope.checked = [];
                        $scope.checkedMsg = [];
                        angular.forEach($scope.brandTable, function (i) {
                            i.checked = true;
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        })
                    }else {
                        angular.forEach($scope.brandTable, function (i) {
                            i.checked = false;
                            $scope.checked = [];
                            $scope.checkedMsg = [];
                        })
                    }
                };
                $scope.selectOne = function ($event) {
                    console.log($event);
                    angular.forEach($scope.brandTable, function (i) {
                        var index = $scope.checked.indexOf(i.id);
                        if(i.checked && index === -1) {
                            $scope.checked.push(i.id);
                            $scope.checkedMsg.push({"id":i.id});
                        } else if (!i.checked && index !== -1){
                            $scope.checked.splice(index, 1);
                            $scope.checkedMsg.splice(index, 1);
                        }
                    });

                    if ($scope.brandTable.length === $scope.checked.length) {
                        $scope.select_all = true;
                    } else {
                        $scope.select_all = false;
                    }
                };
            });
            }
            
        }

        $scope.page = 0;
        $scope.page1 = $scope.page + 1;
        $http({
            url: ngConstant.interfaceHost + 'manager/carBrand/findSearch',
            method: 'GET',
            params:{
                "sort":"leadingChar,asc",
                "page":$scope.page,
                "size":ngConstant.size
            },
            headers: {'Authorization':$cookies.get("token")}
        }).then(function(res){
            console.log(res);
            //页数数据
            $scope.pageMsg = res.data.data;
            $scope.brandTable = res.data.data.content;
            //全选
            //点击下一页上一页时   $scope.select_all初始化置空   在点击事件中写入
            $scope.checked = [];
            $scope.checkedMsg = [];
            $scope.selectAll = function () {
                if($scope.select_all) {
                    $scope.checked = [];
                    $scope.checkedMsg = [];
                    angular.forEach($scope.brandTable, function (i) {
                        i.checked = true;
                        $scope.checked.push(i.id);
                        $scope.checkedMsg.push({"id":i.id});
                    })
                }else {
                    angular.forEach($scope.brandTable, function (i) {
                        i.checked = false;
                        $scope.checked = [];
                        $scope.checkedMsg = [];
                    })
                }
            };
            $scope.selectOne = function ($event) {
                console.log($event);
                angular.forEach($scope.brandTable, function (i) {
                    var index = $scope.checked.indexOf(i.id);
                    if(i.checked && index === -1) {
                        $scope.checked.push(i.id);
                        $scope.checkedMsg.push({"id":i.id});
                    } else if (!i.checked && index !== -1){
                        $scope.checked.splice(index, 1);
                        $scope.checkedMsg.splice(index, 1);
                    }
                });

                if ($scope.brandTable.length === $scope.checked.length) {
                    $scope.select_all = true;
                } else {
                    $scope.select_all = false;
                }
            };




            //下一页
            $scope.nextPageBtn = function(){
                $scope.select_all = false;
                if ($scope.page == $scope.pageMsg.totalPages - 1) {
                    return false;
                } else {
                    $scope.page++;
                    $scope.page1++;
                    getTableData();
                }

            };

            //上一页
            $scope.prevPageBtn = function(){
                $scope.select_all = false;
                if ($scope.page == 0) {
                    return false;
                } else {
                    $scope.page--;
                    $scope.page1--;
                    getTableData();
                }
            };

            //转到第几页
            $scope.myKeyup13 = function(e){
                $scope.select_all = false;
                //IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
                var keycode = window.event?e.keyCode:e.which;
                if(keycode==13){
                    $scope.page = $scope.page1 - 1;
                    getTableData();
                }
            };

            //首页
            $scope.firstPageBtn = function(){
                $scope.select_all = false;
                if ($scope.page == 0) {
                    return false;
                }
                $scope.page = 0;
                $scope.page1 = 1;
                getTableData();
            };

            //尾页
            $scope.lastPageBtn = function(){
                $scope.select_all = false;
                if ($scope.page == $scope.pageMsg.totalPages - 1){
                    return false;
                }
                $scope.page = $scope.pageMsg.totalPages - 1;
                $scope.page1 = $scope.pageMsg.totalPages;
                getTableData();
            };

            //批量删除品牌
            $scope.deleteMerchantBtn = function(){
                if ($scope.checkedMsg.length == 0) {
                    ngAlert.errorMsg('请选择要删除的数据');
                    return false;
                }
                ngAlert.open('确认删除本条记录？');
                console.log($scope.checkedMsg);
                $('.layui-layer-btn').click(function(){
                    $http({
                        url: ngConstant.interfaceHost + 'manager/carBrand/deleteCarBrand',
                        method: 'POST',
                        data:angular.toJson($scope.checkedMsg) ,
                        headers: {'Authorization':$cookies.get("token")}
                    }).then(function(res){
                        ngAlert.successMsg(res.data.data);
                        getTableData();
                    });
                });
            };

            //点击禁用 / 启用
            $scope.disableBtn = function(){
                this.item.dataSign = 1;
                $http({
                    url: ngConstant.interfaceHost + 'manager/carBrand/disable',
                    method: 'POST',
                    data:{"id":this.item.id},
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    console.log(res);
                    ngAlert.successMsg(res.data.data);

                },function(res){
                    ngAlert.errorMsg(res.data.errMsg);
                });
            };
            $scope.enDisableBtn = function(){
                this.item.dataSign = 0;
                $http({
                    url: ngConstant.interfaceHost + 'manager/carBrand/enable',
                    method: 'POST',
                    data:{"id":this.item.id},
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    console.log(res);
                    ngAlert.successMsg(res.data.data);

                },function(res){
                    ngAlert.errorMsg(res.data.errMsg);
                });
            };

        });


        $scope.layerOutShow = false;
        //点击新增按钮进入弹层以后的操作
        $scope.createBrandBtn = function () {
            $scope.layerOutShow = true;

            //弹层中  获取首字母
            $scope.chars = [
                {"leadingChar": "A"},
                {"leadingChar": "B"},
                {"leadingChar": "C"},
                {"leadingChar": "D"},
                {"leadingChar": "E"},
                {"leadingChar": "F"},
                {"leadingChar": "G"},
                {"leadingChar": "H"},
                {"leadingChar": "I"},
                {"leadingChar": "J"},
                {"leadingChar": "K"},
                {"leadingChar": "L"},
                {"leadingChar": "M"},
                {"leadingChar": "N"},
                {"leadingChar": "O"},
                {"leadingChar": "P"},
                {"leadingChar": "Q"},
                {"leadingChar": "R"},
                {"leadingChar": "S"},
                {"leadingChar": "T"},
                {"leadingChar": "U"},
                {"leadingChar": "V"},
                {"leadingChar": "W"},
                {"leadingChar": "X"},
                {"leadingChar": "Y"},
                {"leadingChar": "Z"},
            ];
            $scope.leadingChar = $scope.chars[0].leadingChar;
            console.log($scope.leadingChar);

            $scope.creatBrandMsg = {};
            //弹层中的提交按钮点击事件
            $scope.creatBrandSub = function(){
                var leadObj = document.getElementsByName("leadingCharName")[0];
                var leadLabel= leadObj .options[leadObj.selectedIndex].text;
                $scope.creatBrandMsg.leadingChar = leadLabel;
                console.log($scope.creatBrandMsg);
                $http({
                    url: ngConstant.interfaceHost + 'manager/carBrand/createBrand',
                    method: 'POST',
                    data:$scope.creatBrandMsg,
                    headers: {'Authorization':$cookies.get("token")}
                }).then(function(res){
                    ngAlert.successMsg(res.data.data);
                    // $timeout(function(){
                    //     $scope.layerOutShow = false;
                    // },2000);
                    getTableData();
                },function(res){
                    ngAlert.errorMsg(res.data.errMsg);
                });
            };

        };

        $scope.closeLay = function(){
            $scope.layerOutShow = false;
        }

    })
});